﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace SPThumbnailExtender.Interne
{
    internal class ThumbnailOpenXML:ThumbnailBase
    {
        public ThumbnailOpenXML(Uri uri)
            : base(uri)
        { }
        public ThumbnailOpenXML(Uri uri, int Width, int Height)
            : base(uri, Width, Height)
        { }
        public override System.Drawing.Bitmap GenerateThumbnail()
        {
            return GenerateThumbnail(0);
        }

        public override System.Drawing.Bitmap GenerateThumbnail(int Page)
        {
            Stream strFile = this.GetFileStream();
            string FileExt = Path.GetExtension(this.FileUri.AbsolutePath);
            Image imgThumb = null;
            switch (FileExt)
            {

                case ".docx":
                    {
                        imgThumb = UpdateWordThumbnails(strFile);
                        break;
                    }

                case ".pptx":
                    {
                        imgThumb = UpdatePowerPointThumbnails(strFile);
                        break;
                    }

                case ".xlsx":
                    {
                        imgThumb = UpdateExcelThumbnails(strFile);
                        break;
                    }

            }
            
            return new Bitmap(imgThumb);

        }

        public override void GenerateThumbnails()
        {
            Bitmap bmp=GenerateThumbnail();
            Helper.DeleteThumbnail(this.FileUri, 0);
            Helper.InsertThumbnail(this.FileUri, 0, bmp);
        }

        public override int GetCount()
        {
            throw new NotImplementedException();
        }
        private Image UpdateExcelThumbnails(Stream FileStream)
        {

            ThumbnailPart ThumbnailPart;

            using (SpreadsheetDocument ExcelDocFile = SpreadsheetDocument.Open(FileStream, true))
            {

                ThumbnailPart = ExcelDocFile.ThumbnailPart;

                if (ThumbnailPart != null)
                {

                    Stream ThumbnailStream = ThumbnailPart.GetStream(FileMode.Open, FileAccess.ReadWrite);

                    Metafile SourceThumbnail = new Metafile(ThumbnailStream);

                    return SourceThumbnail;

                }

            }
            return new Bitmap(typeof(ThumbnailOpenXML).Assembly.GetManifestResourceStream("SPThumbnailExtender.no_thumbnail.jpg"));

        }

        private Image UpdatePowerPointThumbnails(Stream FileStream)
        {

            ThumbnailPart ThumbnailPart;

            using (PresentationDocument PPDocFile = PresentationDocument.Open(FileStream, true))
            {

                ThumbnailPart = PPDocFile.ThumbnailPart;

                if (ThumbnailPart != null)
                {

                    Stream ThumbnailStream = ThumbnailPart.GetStream(FileMode.Open, FileAccess.ReadWrite);

                    Bitmap SourceThumbnail = new Bitmap(ThumbnailStream);

                    return SourceThumbnail;

                }

            }
            return new Bitmap(typeof(ThumbnailOpenXML).Assembly.GetManifestResourceStream("SPThumbnailExtender.no_thumbnail.jpg"));

        }

        private Image UpdateWordThumbnails(Stream FileStream)
        {

            ThumbnailPart ThumbnailPart = null;

            using (WordprocessingDocument WPDocFile = WordprocessingDocument.Open(FileStream, true))
            {

                ThumbnailPart = WPDocFile.ThumbnailPart;



                //If we have a thumbnail saved with the document then save it as a small png 

                //the hidden folders (_t and _w) at differnt resolutions underneath the root folder of 

                //the document library

                if (ThumbnailPart != null)
                {

                    //Load a stream with the thumbnail data and create a Metafile object

                    //wmf format in Word and Excel but jpg in PowerPoint

                    Stream ThumbnailStream = ThumbnailPart.GetStream(FileMode.Open, FileAccess.ReadWrite);

                    Metafile SourceThumbnail = new Metafile(ThumbnailStream);

                    return SourceThumbnail;

                }

            }
            return new Bitmap(typeof(ThumbnailOpenXML).Assembly.GetManifestResourceStream("SPThumbnailExtender.no_thumbnail.jpg"));


        }

    }
}
